<?php
class Event extends BaseEntity
{
    protected $_who;
    protected $_eventTime;
    protected $_location;
    protected $_city;
    protected $_description;
    protected $_link;
    protected $_type;
    protected $_creator;

    protected $_name = 'event';

    const TYPE_MUSIC = 0;
    const TYPE_XS = 1;

    public function createMusicEvent($creator, $who, $eventTime, $location, $city, $description='', $link='')
    {
        return $this->createByBiz($creator, self::TYPE_MUSIC, $who, $eventTime, $location, $city, $description, $link);
    }

    private function createByBiz($creator, $type, $who, $eventTime, $location, $city, $description='', $link='')
    {/*{{{*/
        ParamsChecker::requireNotZero($creator, gettext('userId'));
        ParamsChecker::requireNotNull($type, gettext('type'));
        ParamsChecker::requireNotNull($who, gettext('who'));
        ParamsChecker::requireNotNull($eventTime, gettext('time'));
        ParamsChecker::requireNotNull($location, gettext('location'));
        ParamsChecker::requireNotNull($city, gettext('city'));

        $createTime = $lastModifyTime = date('Y-m-d H:i:s');

        $arr['id'] = self::createId();
        $arr['ver'] = self::INITIALIZED_VERSION;
        $arr['createTime'] = $createTime;
        $arr['lastModifyTime'] = $lastModifyTime;

        $arr['creator'] = $creator;
        $arr['type'] = $type;
        $arr['who'] = $who;
        $arr['eventTime'] = $eventTime;
        $arr['location'] = $location;
        $arr['city'] = $city;
        $arr['description'] = $description;
        $arr['link'] = $link;
        return $this->create($arr);
    }/*}}}*/

    public function find($from, $to, $location='')
    {
        $db = &$this->getAdapter();

        $v = array();

        $where = 'eventTime >= :from and eventTime <= :to';
        $v['from'] = $from;
        $v['to'] = $to;

        if ('' != $location)
        {
            $where .= ' and location=:location';
            $v['location'] = $location;
        }

        return $db->fetchAll('select * from '.$this->_name.' where '.$where.' order by eventTime asc', $v);
    }

    public function findByAttender($attenderId)
    {
        ParamsChecker::requireNotZero($attenderId, gettext('userId'));

        $db = &$this->getAdapter();
        return $db->fetchAll('select event.* from event, event_ref where event_ref.userId=? and event.id=event_ref.eventId order by event.eventTime asc', $attenderId);
    }

    public function setEventTime($eventTime)
    {
        $this->_eventTime = $eventTime;
    }

    public function setLocation($location)
    {
        $this->_location = $location;
    }

    public function setCity($city)
    {
        $this->_city = $city;
    }

    public function setDescription($description)
    {
        $this->_description = $description;
    }

    public function setLink($link)
    {
        $this->_link = $link;
    }

    protected function map()
    {/*{{{*/
        return array('creator', 'who', 'eventTime', 'type', 'city', 'location', 'link', 'description');
    }/*}}}*/
}
